# coding=utf-8

import os
from datetime import datetime
# import os.path

def GetExamFiles():
    rs = []
    for f in os.listdir("."):
        # Memory source ends with '.txt' starts with non-digit chars.
        if os.path.isfile(f) and (f.endswith('.txt') and not f[0].isdigit()):
            rs.append(f)
    return rs


def ReadFromFile(filename):
    with open(filename) as f:
        return [x for x in f]


def RemoveContent(rs):
    return [x for x in rs if not x[0].isspace()]


def WriteToFileHandle(file, rs):
    file.write(''.join(rs))
    file.write('\n')


def WriteCheckFile(file1, file2, batch):
    with open(batch, 'w') as fb:
        KDIFF3_CMD = '"C:\Program Files\KDiff3\kdiff3.exe"'
        fb.write('%s %s %s\n' % (KDIFF3_CMD, file1, file2))


if __name__ == "__main__":
    rs = GetExamFiles()
    print(rs)
    DateTimeStr = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
    File1 = "%s_Org.txt" % DateTimeStr
    File2 = "%s_New.txt" % DateTimeStr
    File3 = "%s_Chk.bat" % DateTimeStr
    with open(File1, 'w') as f1, open(File2, 'w') as f2:
        for filename in rs:
            c = ReadFromFile(filename)
            WriteToFileHandle(f1, c)
            c = RemoveContent(c)
            WriteToFileHandle(f2, c)
            WriteCheckFile(File1, File2, File3)
